import editForm from "../form.vue";
import { usePublicHooks } from "../../../../hooks";
import type { FormItemProps } from "./types";
import { addDialog } from "@/components/ReDialog";
import { reactive, onMounted, h, toRaw } from "vue";
import { delay } from "@pureadmin/utils";
import { message } from "@/utils/message";
import { getParknflyConfig, updateParknflyConfig } from "@/api/connect/b2c";

export function parknfyConfig() {
  const form = reactive({
    push_base_url: "",
    secretkey: "",
    appid: "",
    status: -1,
    page: 1,
    limit: 20
  });

  //从父级钩子里取东西
  const { tagStyle, pagination, loadingConfig, formRef, dataList, loading } =
    usePublicHooks();

  const columns: TableColumnList = [
    {
      label: "id",
      prop: "id",
      width: 100
    },
    {
      label: "推送url",
      prop: "push_base_url",
      width: 300
    },
    {
      label: "secretkey",
      prop: "secretkey",
      width: 300
    },
    {
      label: "appid",
      prop: "appid",
      width: 200
    },
    {
      label: "状态",
      prop: "status",
      cellRenderer: ({ row, props }) => (
        <el-tag size={props.size} style={tagStyle.value(row.status)}>
          {row.status === 1 ? "启用" : "停用"}
        </el-tag>
      )
    },
    {
      label: "操作",
      fixed: "right",
      slot: "operation"
    }
  ];

  async function onSearch() {
    loading.value = true;
    const { data } = await getParknflyConfig(toRaw(form));
    console.log(data);
    console.log(dataList);

    dataList.value = data.list;
    pagination.total = data.total;
    pagination.pageSize = data.pageSize;
    pagination.currentPage = data.currentPage;

    setTimeout(() => {
      loading.value = false;
    }, 500);
  }

  function openDialog(title = "修改", row?: FormItemProps) {
    addDialog({
      title: `${title}配置`,
      props: {
        formInline: {
          id: row?.id ?? "",
          push_base_url: row?.push_base_url ?? "",
          secretkey: row?.secretkey ?? "",
          appid: row?.appid ?? "",
          status: row?.status ?? 1
        }
      },
      width: "40%",
      draggable: true,
      fullscreenIcon: true,
      closeOnClickModal: false,
      contentRenderer: () => h(editForm, { ref: formRef }),
      beforeSure: (done, { options }) => {
        const FormRef = formRef.value.getRef();
        const curData = options.props.formInline as FormItemProps;
        function chores() {
          message(`您${title}了ID为${curData.id}的这条数据`, {
            type: "success"
          });
          done(); // 关闭弹框
          onSearch(); // 刷新表格数据
        }
        FormRef.validate(valid => {
          if (valid) {
            console.log("curData", curData);
            // 表单规则校验通过
            if (title === "修改") {
              updateParknflyConfig(curData).then(() => {
                // 实际开发先调用新增接口，再进行下面操作
                chores();
              });
            }
          }
        });
      }
    });
  }

  //页面加载
  onMounted(() => {
    onSearch();
  });

  //页面条数变化
  function onSizeChange(val) {
    form.limit = val;
    onSearch();
  }

  //页码变化
  function onCurrentChange(val) {
    loadingConfig.text = `正在加载第${val}页...`;
    loading.value = true;
    delay(600).then(() => {
      form.page = val;
      onSearch();

      //刷新数据
      loading.value = false;
    });
  }

  //重置表单
  function resetForm(formEl) {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  }

  return {
    columns,
    openDialog,
    form,
    loading,
    onSearch,
    resetForm,
    dataList,
    pagination,
    onSizeChange,
    onCurrentChange,
    loadingConfig
  };
}
